package com.lzq.course.business.controller.admin;

import com.lzq.course.server.dto.*;
import com.lzq.course.server.service.CourseCategoryService;
import com.lzq.course.server.service.CourseService;
import com.lzq.course.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/course")
public class CourseController {

    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;

    @Resource
    private CourseCategoryService courseCategoryService;

    /**
     * 查询列表 分页查询
     * @param pageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody CoursePageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        courseService.list(pageDto);

        responseDto.setContent(pageDto);
        return responseDto;
    }

    @PostMapping("/list-category/{courseId}")
    public ResponseDto listCategory(@PathVariable String courseId) {
        ResponseDto responseDto = new ResponseDto();
        List<CourseCategoryDto> dtoList = courseCategoryService.listByCourse(courseId);

        responseDto.setContent(dtoList);
        return responseDto;
    }

    /**
     * 保存
     * @param courseDto
     * @return
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody CourseDto courseDto) {

        //保存校验
        ValidatorUtil.require(courseDto.getName(), "名称");
        ValidatorUtil.length(courseDto.getName(),"名称", 1, 50);
        ValidatorUtil.length(courseDto.getSummary(),"概述", 1, 2000);
        ValidatorUtil.length(courseDto.getImage(),"封面", 1, 100);

        ResponseDto responseDto = new ResponseDto();
        courseService.save(courseDto);

        responseDto.setContent(courseDto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        courseService.delete(id);

        return responseDto;
    }

    /**
     * 查询课程内容
     * @param id
     * @return
     */
    @GetMapping("find-content/{id}")
    public ResponseDto findContent(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();

        CourseContentDto content = courseService.findContent(id);
        responseDto.setContent(content);

        return responseDto;
    }

    /**
     * 新增 修改 内容
     * @param contentDto
     * @return
     */
    @PostMapping("save-content")
    public ResponseDto saveContent(@RequestBody CourseContentDto contentDto) {
        ResponseDto responseDto = new ResponseDto();

        int i = courseService.saveContent(contentDto);

        return responseDto;
    }

    /**
     * 课程自动更新排序
     * @param sortDto
     * @return
     */
    @PostMapping("update-sort")
    public ResponseDto sort(@RequestBody SortDto sortDto) {
        ResponseDto responseDto = new ResponseDto();

        courseService.sort(sortDto);

        return responseDto;
    }
}
